package com.example.douyin.zhibo;

import android.content.Context;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.example.lib_base_mvvm.entity.GiftEntity;

import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.ConsoleHandler;


/**
 * 礼物控制类，为了调用更新giftLayout
 */
public class GiftControl {
    GiftLayout giftLayout;
    private Context context;
    private int count = 0;//发送礼物的数量
    private long lastTime;//记录上次发送礼物的时间，为了判断是否是连续发送
    Timer timer;
    Handler handler = new Handler(){};

    public GiftControl(GiftLayout giftLayout, Context context) {
        this.giftLayout = giftLayout;
        this.context = context;
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(System.currentTimeMillis()-lastTime>3000){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            giftLayout.setVisibility(View.GONE);//超过3秒不再算连点
                            count = 0;
                        }
                    });
                }
            }
        },0,1000);
    }
    //加载礼物--当发送礼物时调用
    public void loadGift(GiftEntity.DataBean dataBean){
        if(count==0){//第一次发送
            giftLayout.setVisibility(View.VISIBLE);
            count++;
            lastTime = System.currentTimeMillis();
        }else if(System.currentTimeMillis()-lastTime<2000){//连续发送
            count++;
            lastTime = System.currentTimeMillis();
        }

        giftLayout.loadCount(count);
        giftLayout.loadName(SPUtils.getInstance().getString("username"));
        giftLayout.loadTou(SPUtils.getInstance().getString("icon"));
        giftLayout.loadGift(dataBean);
    }

}
